﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace QL_Truong_Hoc.HocTap
{
    public partial class frmQuanLyDiemVanHoa : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "diem_hoc";
        private bool mblnIsValid = false;

        private int mintKVH;
        private int mintLopID;
        private int mintMonID;
        private int mintCapDoID;
        private string mstrMon;
        private string mstrCapDo;
        private string mstrLop;
        private string mstrKhoas;

        public frmQuanLyDiemVanHoa()
        {
            InitializeComponent();
        }

        private void frmQuanLyDiemVanHoa_Load(object sender, EventArgs e)
        {
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }

            // TODO: This line of code loads data into the 'qLTHDataSet.mCapDoVanHoa' table. You can move, or remove it, as needed.
            this.mCapDoVanHoaTableAdapter.Fill(this.qLTHDataSet.mCapDoVanHoa);
            //// TODO: This line of code loads data into the 'qLTHDataSet.tHocSinhMonVanHoaGetFull' table. You can move, or remove it, as needed.
            //this.tHocSinhMonVanHoaGetFullTableAdapter.Fill(this.qLTHDataSet.tHocSinhMonVanHoaGetFull);
            // TODO: This line of code loads data into the 'qLTHDataSet.mKhungVanHoaGetFull' table. You can move, or remove it, as needed.
            this.mKhungVanHoaGetFullTableAdapter.Fill(this.qLTHDataSet.mKhungVanHoaGetFull);
        }

        private void grdControlLop_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (!validControlBlank.Validate()) return;

                mintKVH = clsCommon.fncCnvNullToInt(dgvLopVanHoa.GetFocusedRowCellValue(colKVH_ID));

                if (mintKVH <= 0) return;

                Cursor.Current = Cursors.WaitCursor;

                mintLopID = clsCommon.fncCnvNullToInt(dgvLopVanHoa.GetFocusedRowCellValue(colLVH_ID));
                mintMonID = clsCommon.fncCnvNullToInt(dgvLopVanHoa.GetFocusedRowCellValue(colMVH_ID));
                mintCapDoID = clsCommon.fncCnvNullToInt(lokCapDoVanHoa.EditValue);
                mstrMon = clsCommon.fncCnvNullToString(dgvLopVanHoa.GetFocusedRowCellValue(colMVH_ten));
                mstrCapDo = lokCapDoVanHoa.Text;
                mstrLop = clsCommon.fncCnvNullToString(dgvLopVanHoa.GetFocusedRowCellValue(colLVH_ten));
                mstrKhoas = clsCommon.fncCnvNullToString(dgvLopVanHoa.GetFocusedRowCellValue(colkhs_tenkhoas));

                grpMonHoc.Text = string.Format("Khóa {0} - Lớp {1} - Môn {2} - {3}", mstrKhoas, mstrLop, mstrMon, mstrCapDo);

                // TODO: This line of code loads data into the 'qLTHDataSet.tHocSinhMon_GetFull' table. You can move, or remove it, as needed.
                this.tHocSinhMonVanHoaGetFullTableAdapter.Fill(this.qLTHDataSet.tHocSinhMonVanHoaGetFull, mintLopID, mintMonID, mintCapDoID);

            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                dgvDiem.UpdateCurrentRow();
                if (!mblnIsValid) return;

                Cursor.Current = Cursors.WaitCursor;

                DTO.THocSinhMonVanHoaDTO objHsMon;
                bool blnSuccess = true;
                bool blnDiemCoDinh = false;
                int intHsID;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    for (int i = 0; i <= dgvDiem.RowCount - 1; i++)
                    {
                        if (!blnSuccess) break;

                        //nếu là điểm cố định thì ko cập nhật nữa.
                        blnDiemCoDinh = clsCommon.fncCnvNullToBool(dgvDiem.GetRowCellValue(i, coldiemcodinh));
                        if (blnDiemCoDinh) continue;

                        intHsID = clsCommon.fncCnvNullToInt(dgvDiem.GetRowCellValue(i, colHS_ID));
                        objHsMon = DAO.THocSinhMonVanHoaDAO.Select(Program.gobjDb, intHsID, mintMonID, mintCapDoID);
                        objHsMon.Diemheso1_1 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso1_1));
                        objHsMon.Diemheso1_2 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso1_2));
                        objHsMon.Diemheso1_3 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso1_3));

                        objHsMon.Diemheso2_1 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso2_1));
                        objHsMon.Diemheso2_2 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso2_2));
                        objHsMon.Diemheso2_3 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso2_3));

                        objHsMon.Diemheso3 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso3));
                        objHsMon.Diemtongket = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemtongket));

                        blnSuccess = blnSuccess && DAO.THocSinhMonVanHoaDAO.Update(Program.gobjDb, Program.gobjTrans, objHsMon);
                    }

                    //commit dữ liệu nếu không có gì lỗi, ngược lại thì rollback
                    if (blnSuccess) { Program.gobjTrans.Commit(); }
                    else
                    { Program.gobjTrans.Rollback(); }
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess) { clsCommon.fncMsgInfo("Cập nhật thành công."); }
                else { clsCommon.fncMsgError("Cập nhật thất bại. Xin hãy thử lại."); }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void dgvDiem_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                string strColName = "";
                float fltDiem = 0.0F;
                mblnIsValid = true;

                for (int i = 1; i <= 3; i++)
                {
                    strColName = "diemheso1_";
                    strColName += i.ToString();

                    if (!float.TryParse(dgvDiem.GetRowCellValue(e.RowHandle, strColName).ToString(), out fltDiem))
                    {
                        e.Valid = false;
                        dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                        mblnIsValid = false;
                        return;
                    }

                    if (fltDiem < 0 || fltDiem > 10)
                    {
                        e.Valid = false;
                        dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                        mblnIsValid = false;
                        return;
                    }
                }

                for (int i = 1; i <= 3; i++)
                {
                    strColName = "diemheso2_";
                    strColName += i.ToString();

                    if (!float.TryParse(dgvDiem.GetRowCellValue(e.RowHandle, strColName).ToString(), out fltDiem))
                    {
                        e.Valid = false;
                        dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                        mblnIsValid = false;
                        return;
                    }

                    if (fltDiem < 0 || fltDiem > 10)
                    {
                        e.Valid = false;
                        dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                        mblnIsValid = false;
                        return;
                    }
                }

                strColName = "diemheso3";
                if (!float.TryParse(dgvDiem.GetRowCellValue(e.RowHandle, strColName).ToString(), out fltDiem))
                {
                    e.Valid = false;
                    dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                    mblnIsValid = false;
                    return;
                }

                if (fltDiem < 0 || fltDiem > 10)
                {
                    e.Valid = false;
                    dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                    mblnIsValid = false;
                    return;
                }

                strColName = "diemtongket";
                if (!float.TryParse(dgvDiem.GetRowCellValue(e.RowHandle, strColName).ToString(), out fltDiem))
                {
                    e.Valid = false;
                    dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                    mblnIsValid = false;
                    return;
                }

                if (fltDiem < 0 || fltDiem > 10)
                {
                    e.Valid = false;
                    dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                    mblnIsValid = false;
                    return;
                }

            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvDiem_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            try
            {
                e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void lokCapDoVanHoa_EditValueChanged(object sender, EventArgs e)
        {
            grdControlLop_MouseDoubleClick(null, null);
        }

        private void btnTinhDiemTongKet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                float fltHS11;
                float fltHS12;
                float fltHS13;
                float fltHS21;
                float fltHS22;
                float fltHS23;
                float fltHS3;
                float fltTongket;
                bool blnDiemCoDinh;

                for (int i = 0; i <= dgvDiem.RowCount - 1; i++)
                {
                    //nếu là điểm nhập tay do hs đã có điểm tổng kết thì ko tính lại nữa.
                    blnDiemCoDinh = clsCommon.fncCnvNullToBool(dgvDiem.GetRowCellValue(i, coldiemcodinh));
                    if (blnDiemCoDinh) continue;

                    //tính điểm
                    fltHS11 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso1_1));
                    fltHS12 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso1_2));
                    fltHS13 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso1_3));

                    fltHS21 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso2_1));
                    fltHS22 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso2_2));
                    fltHS23 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso2_3));

                    fltHS3 = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, coldiemheso3));

                    fltTongket = clsCommon.fncDiemTongKetVanHoa(fltHS11, fltHS12, fltHS13, fltHS21, fltHS22, fltHS23, fltHS3);
                    dgvDiem.SetRowCellValue(i, coldiemtongket, fltTongket);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

    }
}